package com.itcast.cn.conversation.cookie;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Cookie不可以直接存储中文, 这里使用 URLEncoder 编码
        String value = "张三";
        String encode = URLEncoder.encode(value, "utf-8");
        // 存储到Cookie中
        Cookie cookie = new Cookie("username", encode);
        // 参数为秒 设置cookie的存活时间
        // cookie.setMaxAge(60 * 60 * 24 * 7);
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
